package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Notice;
import com.gaohe.pojo.Orders;
import com.gaohe.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @GetMapping("/page")
    public R page(@RequestParam int page,
                  @RequestParam int pageSize,
                  @RequestParam(required = false) Integer status){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        QueryWrapper<Orders> qw = new QueryWrapper<>();
        qw.select(!StringUtils.isEmpty(status),"SELECT orders.*,order_detail.create_time,order_detail.end_time,order_detail.description from " +
                "orders LEFT JOIN order_detail on orders.order_id=order_detail.id where orders.status=1");
        Page<Orders> page1 = ordersService.page(ordersPage, qw);
        return R.success(ordersPage);
    }
    @GetMapping
    public R getOrders(@RequestParam Long userId){
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(Orders::getUserId,userId);
        List<Orders> list = ordersService.list(qw);
        return R.success(list);
    }
    @DeleteMapping
    public R deleteOrders(@RequestParam Long id){
        boolean b = ordersService.removeById(id);
        return b?R.success("删除成功"):R.error("删除失败");
    }

}
